$(function () {
    let PassWord = '';
    let AjaxToken = '';
    // cookie 的 增添改查 对象
    const CookieUtil = {
        // 获取cookie
        get: function (name) {
            var cookieName = encodeURIComponent(name) + "=",
                cookieStart = document.cookie.indexOf(cookieName),
                cookieValue = null;
            if (cookieStart > -1) {
                var cookieEnd = document.cookie.indexOf(":", cookieStart);
                if (cookieEnd === -1) {
                    cookieEnd = document.cookie.length;
                }
                cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
            }
            return cookieValue;
        },
        // 设置创建 写入 cookie
        set: function (name, value, expires, path, domain, secure) {
            var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
            if (expires instanceof Date) {
                cookieText += "; expires =" + expires.toGMTString();
            }
            if (path) {
                cookieText += "; path =" + path;
            }
            if (domain) {
                cookieText += "; secure;"
            }
            document.cookie = cookieText;
        },
        // 删除cookie
        unset: function (name, path, domain, secure) {
            this.set(name, "", new Date(0), path, domain, secure);
        }
    };
    const main = {
        // 清除按钮
        clear: function () {
            main.clearId();
            main.clearPassWord();
        },
        // 移除密码
        clearPassWord: function () {
            $('.clear-password').on("click", function () {
                $('.password').val('');
                main.ButtonOutStatus();
            });
        },
        // 清除id
        clearId: function () {
            $('.clear-ID').on("click", function () {
                $(".ID").val('');
                main.ButtonOutStatus();
            });
        },
        // 判断是否密码展示
        isPassWordShow: function () {
            $('.show-PassWord').on('click', function () {
                if ($(this).hasClass('show-PassWord2')) {
                    main.hidePassWord();
                } else {
                    main.showPassWord();
                }
            });
        },
        // 展示密码
        showPassWord: function () {
            $('.show-PassWord').addClass('show-PassWord2');
            $('.password').attr('type', 'text');
        },
        // 隐藏密码
        hidePassWord: function () {
            $('.show-PassWord').removeClass('show-PassWord2');
            $('.password').attr('type', 'password');
        },
        // 判断是否又帐号,没有改变按钮状态
        isInputId: function () {
            $('.ID').on("input", function () {
                if ($(this).val().length > 0 && $('.password').val().length > 0) {
                    main.ButtonInStatus();
                } else {
                    main.ButtonOutStatus();
                }
            });
        },
        // 判断是否有密码,没有改变按钮状态
        isInputPassWord: function () {
            $('.password').on("input", function () {
                // 记下密码
                PassWord = $(this).val();
                if ($(this).val().length > 0 && $('.ID').val().length > 0) {
                    main.ButtonInStatus();
                } else {
                    main.ButtonOutStatus();
                }
            });
        },
        // 按钮可点击
        ButtonInStatus: function () {
            $('.login-button').addClass('login-button2');
        },
        // 按钮不可点击
        ButtonOutStatus: function () {
            $('.login-button').removeClass('login-button2');
        },
        // 按钮 状态判断
        loginButtonInStatus: function () {
            // 输入账号
            main.isInputId();
            // 输入密码
            main.isInputPassWord();
            // 按钮可选 状态
            main.ButtonInStatus();
            // 按钮不可选 状态
            main.ButtonOutStatus();
        },
        // 登入按钮登入
        login: function () {
            $('.login-button').on("click", function () {
                if ($('.login-button').hasClass('login-button2')) {
                    // 校验 账号密码
                    main.verifyCode();
                } else {
                    // 如果没有输入账号点确定
                    if ($('.ID').val().length === 0) {
                        $('#omitId').fadeIn();
                        setTimeout(function () {
                            $('#omitId').fadeOut();
                        }, 2000);
                    }
                    //如果没有输入密码点击确定
                    else if ($('.password').val().length === 0) {
                        $('#omitPassWord').fadeIn();
                        setTimeout(function () {
                            $('#omitPassWord').fadeOut();
                        }, 2000);
                    }
                }
            });
        },
        // 获取校验码
        verifyCode: function () {
            $.ajax({
                url: "https://www.easy-mock.com/mock/5ad9997a9920725e3e3e3146/password",
                async: 'false',
                success: function (s) {
                    main.disposeData(s);
                }
            });
        },
        // 将获取的数据赋值到let 中
        disposeData: function (s) {
            if (s.data.passWord.passWord === PassWord || s.data.passWord2.passWord) {
                if (s.data.passWord.passWord === PassWord) {
                    AjaxToken = s.data.passWord.token;
                } else {
                    AjaxToken = s.data.passWord2.token;
                }
                main.SaveToSession(AjaxToken);
                alert('登录成功');
                window.history.back();
            } else {
                alert('登录失败');
            }
        },
        // 将账号与密码保存到sessionStorage中
        SaveToSession: function (token) {
            // sessionStorage.setItem('Token', token);
            CookieUtil.set('Token', token);
            console.log('拿到token:' + token);
        },
        // 主干
        master: function () {
            // 移除输入框内容
            main.clear();
            // 登入按钮 状态 改变
            main.loginButtonInStatus();
            // 密码是否展示
            main.isPassWordShow();
            // 登入
            main.login();
        }
    };
    main.master();
});

